import { getAdjCharInfoDetail, updateModelData } from '@/services/adjusttask';
import { getCostCenter } from '@/services/addemp';

import { message } from 'antd';
import { matchDict } from '@/utils/utils';
import { routerRedux } from 'dva/router';
import _ from 'lodash';

export default {
  state: {
    // adjustInfo: {
    updateBaseInfo: {},
    updateCostGenter: {},
    updatePdAmo: [],
    costCenterData: [],
    // },
  },
  effects: {
    // 获取是否进去下一步
    *fetchNextStep({ payload }, { call }) {
      const response = yield call(getAdjCharInfoDetail, payload);
      return response;
    },
    // 人员及收费
    *fetchAdjCharInfoDetail(
      {
        payload: { sendId, custId },
      },
      { call, put }
    ) {
      const response = yield call(getAdjCharInfoDetail, { sendId });
      const costCenterRep = yield call(getCostCenter, { custId });
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
      if (costCenterRep.success) {
        yield put({
          type: 'costCenterData',
          payload: costCenterRep.data,
        });
      } else {
        message.warning(response.message);
      }
    },

    submit:[
      function* ({ payload }, { call, put }) {
      const response = yield call(updateModelData, payload);
      if (response.success) {
        yield put(
          routerRedux.push({
            pathname: '/customservicemanage/adjusttask/adjustfinish',
          })
        );
      } else {
        message.warning(response.message);
      }
    },
    { type:'throttle',ms:10000 }
    ],
  },

  reducers: {
    updateModelData(state, { payload }) {
      payload.updatePdAmo &&
        payload.updatePdAmo.map((item, i) => {
          item.serAmoFlag = false;
        });

      return {
        ...state,
        ...payload,
      };
    },
    changeQute(state, { payload }) {
      state.updatePdAmo.map((item, i) => {
        if (i === payload.id) {
          item.serAmoFlag = payload.checked;
        }
      });
      return {
        ...state,
        //  ...payload,
      };
    },
    costCenterData(state, { payload }) {
      return {
        ...state,
        costCenterData: payload,
      };
    },
  },
};
